<template>
  <div class="page-content">
    <HyTable
      :Listdata="dataList"
      v-bind="contentTableConfig"
      @queryBtnClick="handleQueryBtnClick"
      @resetBtnClick="handleResetBtnClick"
      :ListCount="dataCount"
      v-model:page="pageInfo"
    >
      <!-- 1.header中的插槽 -->
      <template #header>
        <el-row type="flex" justify="space-between" class="header">
          <div class="title">{{ contentTableConfig?.title }}</div>
          <div>
            <el-button type="primary" @click="handleNewClick">新增数据</el-button>
          </div>
        </el-row>
      </template>
      <!-- 2 列表中的插槽 -->
      <template #status="scope">
        <el-button
          plain
          size="small"
          :type="scope.row.status || scope.row.enable ? 'success' : 'danger'"
        >
          {{ scope.row.status || scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
      <!-- <template #updateAt="scope"> {{ scope }} </template> -->
      <template #createAt="scope">
        <span>
          {{ $filters.formaTime(scope.row.createAt) }}
        </span>
      </template>
      <template #updateAt="scope">
        <span>
          {{ $filters.formaTime(scope.row.createAt) }}
        </span>
      </template>
      <template #handler="{ row }">
        <div class="hanle-btns">
          <el-link v-if="isUpdate" type="primary" @click="handleEditClick(row)">编辑</el-link>
          <el-link v-if="isDelete" type="danger" @click="handleDeleClick(row)">删除</el-link>
        </div>
      </template>

      <!-- 在page-content中动态插入剩余的插槽 -->
      <template v-for="item in otherPropSlots" :key="item.prop" #[item.slotName]="scope">
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </HyTable>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useStore } from '@/store'
import HyTable from '@/base-ui/table'
import { usePermission } from '@/hooks/usePemissions'
export default defineComponent({
  components: {
    HyTable
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    contentTableConfig: {
      type: Object,
      require: true
    },
    pageName: {
      type: String,
      require: true
    }
  },
  emits: ['newBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    const handleResetBtnClick = () => {
      return
    }
    const handleQueryBtnClick = (item: any) => {
      console.log(item)
    }

    const store = useStore()
    // 0.获取操作的权限
    let isCreate = false
    let isUpdate = false
    let isDelete = false
    let isQurey = false
    if (props.pageName) {
      isCreate = usePermission(props.pageName, 'create')
      isUpdate = usePermission(props.pageName, 'update')
      isDelete = usePermission(props.pageName, 'delete')
      isQurey = usePermission(props.pageName, 'query')
    }
    // 双向绑定pageInfo
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    watch(pageInfo, () => {
      getPageData()
    })
    // 发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,

        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }

    getPageData()

    // 从vuex 中获取请求
    const dataList = computed(() => store.getters[`system/pageListData`](props.pageName))
    const dataCount = computed(() => store.getters[`system/pageListCount`](props.pageName))

    // 4 获取其他的动态插槽名称
    const otherPropSlots = props.contentTableConfig?.propList.filter((item: any) => {
      if (item.slotName == 'status') return false
      if (item.slotName == 'createAt') return false
      if (item.slotName == 'updateAt') return false
      if (item.slotName == 'handler') return false
      return true
    })

    // 5 删除/ 编辑 / 新建操作
    const handleDeleClick = (item: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        ...item
      })
    }
    const handleNewClick = () => {
      emit('newBtnClick')
    }

    const handleEditClick = (item: any) => {
      emit('editBtnClick', item)
    }

    return {
      handleResetBtnClick,
      dataList,
      handleQueryBtnClick,
      getPageData,
      dataCount,
      pageInfo,
      otherPropSlots,
      isCreate,
      isUpdate,
      isDelete,
      isQurey,
      handleDeleClick,
      handleNewClick,
      handleEditClick
    }
  }
})
</script>

<style scoped lang="less">
.header {
  .title {
    font-size: 18px;
    margin: 20px;
    color: skyblue;
    font-weight: 600;
  }
}

::v-deep .hanle-btns {
  display: flex;
  justify-content: center;
  .el-link {
    padding: 0 10px;
  }
}
</style>
